﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutomatedTestDataManagement.Models;

namespace AutomatedTestDataManagement.Controllers
{
    public class TestScenarioController : Controller
    {
        private ApplicationTestDataEntities db = new ApplicationTestDataEntities();

        //
        // GET: /TestScenario/

        public ActionResult Index()
        {
            return View(db.TestScenarios.ToList());
        }

        //
        // GET: /TestScenario/Details/5

        public ActionResult Details(int id = 0)
        {
            TestScenario testscenario = db.TestScenarios.Find(id);
            if (testscenario == null)
            {
                return HttpNotFound();
            }
            return View(testscenario);
        }

        //
        // GET: /TestScenario/Create

        public ActionResult Create(int id = 0)
        {
            if (id != 0) { ViewBag.SuiteID = id; }
            return View();
        }

        //
        // POST: /TestScenario/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(TestScenario testscenario)
        {
            if (ModelState.IsValid)
            {
                db.TestScenarios.Add(testscenario);
                db.SaveChanges();
                if (Request.Form["SuiteID"] != null)
                {
                    return RedirectToAction("Create", "SuiteScenarios", new { id = Request.Form["SuiteID"] });
                }
                else { return RedirectToAction("Index"); }
            }
            if (Request.Form["SuiteID"] != null) { ViewBag.SuiteID = Request.Form["SuiteID"]; }

            return View(testscenario);
        }

        //
        // GET: /TestScenario/Edit/5

        public ActionResult Edit(int id = 0, int suiteID = 0)
        {
            TestScenario testscenario = db.TestScenarios.Find(id);
            if (testscenario == null)
            {
                return HttpNotFound();
            }

            if (suiteID != 0)
            {
                ViewBag.SuiteID = suiteID;
            }

            testscenario.TestScenarioTests = testscenario.TestScenarioTests.OrderBy(t => t.TestNumber).ToList();
            return View(testscenario);
        }

        //
        // POST: /TestScenario/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(TestScenario testscenario)
        {
            if (ModelState.IsValid)
            {
                db.Entry(testscenario).State = EntityState.Modified;
                db.SaveChanges();

                foreach (TestScenarioTest test in testscenario.TestScenarioTests)
                {
                    db.Entry(test).State = EntityState.Modified;
                    db.SaveChanges();
                }

                if (HttpContext.Request.Form["SuiteID"] != null)
                {
                    return RedirectToAction("Edit", "Suite", new { id = HttpContext.Request.Form["SuiteID"] });
                }
                return RedirectToAction("Index");
            }
            if (Request.Form["SuiteID"] != null) { ViewBag.SuiteID = Request.Form["SuiteID"]; }
            return View(testscenario);
        }

        //
        // GET: /TestScenario/Delete/5

        public ActionResult Delete(int id = 0)
        {
            TestScenario testscenario = db.TestScenarios.Find(id);
            if (testscenario == null)
            {
                return HttpNotFound();
            }

            if (testscenario.TestScenarioTests.Count != 0)
            {
                ModelState.AddModelError("", "This test scenario has associated tests. You must remove all linked tests before deleting");
            }

            if (testscenario.TestSuiteScenarios.Count != 0)
            {
                ModelState.AddModelError("", "This test scenario is used in at least one test suite. You must remove it from all test suites before deleting");
            }
            return View(testscenario);
        }

        //
        // POST: /TestScenario/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            TestScenario testscenario = db.TestScenarios.Find(id);
            db.TestScenarios.Remove(testscenario);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}